//usart.c
#include <system.h>

#ifndef test_bit 
#define test_bit(x,y) (x&(1<<y))
#endif

//----------------------------------------------------------------------------
// Writes a string (character array) to the USART
//----------------------------------------------------------------------------
void writeString(const char *s)
{
    char i=0;

    while( s[i]!=0 ){
        // write 'u' to serial port, LCD, etc.
        while( ! test_bit(pir1, TXIF) )
			clear_wdt();

        txreg=s[i];
        i++;
    }
}

//----------------------------------------------------------------------------
// Writes a single character to the USART
//----------------------------------------------------------------------------
void drainTx()
{
    while( ! test_bit(pir1, TXIF) ){
        clear_wdt();
    }
}	
void sendChar(char c)
{

    while( ! test_bit(pir1, TXIF) ){
        clear_wdt();
    }
    txreg=c;
}

void sendManchesterByte(char c)
{
	int i,j,me;

	for (i=0; i<2; i++) { 
		me = 0;         // manchester encoded txbyte
		for (j=0 ; j<4; j++) {
			me >>=2;
			if (test_bit(c,0) )
				me |= 0x40; // 1->0
			else
				me |= 0x80; // 0->1
			c >>=1;
        }
        sendChar(me);
    }
}

//----------------------------------------------------------------------------
// Writes a 16-bit integer in decimal format to the USART
//----------------------------------------------------------------------------
void sendDecimalShort(unsigned short n)
{
    if( n >=10000 )
        sendChar(((n/10000)%10)+'0');
    if( n >=1000 )
        sendChar(((n/1000)%10)+'0');
    if( n >=100 )
        sendChar(((n/100)%10)+'0');
    if( n >=10 )
        sendChar(((n/10)%10)+'0');
    sendChar((n%10)+'0');
}

//----------------------------------------------------------------------------
// Writes a 16-bit integer in decimal format to the USART
//----------------------------------------------------------------------------
void sendDecimalShortPad(unsigned short n, char pad)
{
    if( n >=10000 )
        sendChar(((n/10000)%10)+'0');
    else if(pad >= 4)
        sendChar(' ');
    if( n >=1000 )
        sendChar(((n/1000)%10)+'0');
    else if(pad >= 3)
        sendChar(' ');
    if( n >=100 )
        sendChar(((n/100)%10)+'0');
    else if(pad >= 2)
        sendChar(' ');
    if( n >=10 )
        sendChar(((n/10)%10)+'0');
    else if(pad >= 1)
        sendChar(' ');
    sendChar((n%10)+'0');
}

//----------------------------------------------------------------------------
// Writes a 8-bit byte in decimal format to the USART
//----------------------------------------------------------------------------
void sendDecimalByte(char n)
{
    if( n >=100 )
        sendChar(((n/100)%10)+'0');
    if( n >=10 )
        sendChar(((n/10)%10)+'0');
    sendChar((n%10)+'0');
}

//----------------------------------------------------------------------------
// Writes a 8-bit byte in hex format to the USART
//----------------------------------------------------------------------------
void sendHexByte(char n)
{
    char h;
    h=(n>>4)&0xF;
    n=n&0xF;
    if( h>=10 )
        sendChar((h-10)+'A');
    else
        sendChar(h+'0');
    if( n>=10 )
        sendChar((n-10)+'A');
    else
        sendChar(n+'0');
}

//----------------------------------------------------------------------------
// Writes a 16-bit integer in hex format to the USART
//----------------------------------------------------------------------------
void sendHexInt(int n)
{
    sendHexByte((n>>8)&0xFF);
    sendHexByte(n&0xFF);
}

//----------------------------------------------------------------------------
// Writes a 8-bit byte in binary format to the USART
//----------------------------------------------------------------------------
void sendBinaryByte(char n)
{
    static char masks[]={0x80, 0x40, 0x20, 0x10, 8,4,2,1 };
    int i;
    for(i=0; i<8; i++ ){
        if( n & masks[i] )
            sendChar('1');
        else
            sendChar('0');
    }
}
